<?php
require_once('ModelCreationException.php');
/**
 * 
 * The Model class is meant to be extended by any of the Model components of jankCMS
 * which require access to the data source of the application. (i.e. any "Factory") 
 * components. It is not meant to be instantiated directly, hence the protected 
 * constructor. 
 * 
 * @author Rick Hutcheson
 */
class Model {
	/**
	 * 
	 * A handle to the underlying database, unitialized until needed. 
	 * @var PDO $database
	 */
	protected $database;
	
	/**
	 * 
	 * Creates an initial Model with a connection to its data source. If the data source
	 * is unavailable or not configured properly, then a ModelCreationException is thrown.
	 * @throws ModelCreationException
	 */
	protected function __construct() {
		//Put the type of database into the configuration file.
		try { 
			$this->database = new PDO("mysql:host = 'localhost';dbname=jank_test", 'root', '');
		} catch (PDOException $pe) {
			throw new ModelCreationException(
								"An error occurred when connecting to database", 0, $pe); 
		}
	}
	
	/**
	 * 
	 * Closes the connection to any underlying datastore.
	 */
	protected function __destruct() {
		//Closes a PDO connection
		$database = null;
	}
}